﻿using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Logging;
using NetTaste;
using Newtonsoft.Json.Linq;
using SqlSugar;
using System.Data;
using System.Linq.Expressions;
using System.Numerics;
using System.Security.Cryptography;
using System.Security.Policy;
using System.Text.RegularExpressions;
using VM.Common.DataUntil;

using VM.Common.Result;
using VM.DAL;
using VM.Entity.EntityMap.Condition;
using VM.Entity.EntityMap.Files;
using VM.Entity.EntityMap.Member;
using VM.Entity.EntityMap.MsgCode;
using VM.Entity.EntityMap.Region;
using VM.Entity.EntityMap.Title;
using VM.Entity.EntityMap.Token;
using VM.Entity.EntityMap.Topic;
using VM.IBLL.Files;
using VM.IBLL.Member;
using VM.IBLL.Token;
using VM.IBLL.Title;
using VM.IDAL;
using static Dm.net.buffer.ByteArrayBuffer;
using System.Drawing.Printing;
using System.Xml.Linq;
using System.Drawing.Drawing2D;
using VM.Entity.EntityDto.TitleDto;

namespace VM.BLL.Title
{
    public class TitleBLL : BaseBLL, ITitleBLL
    {

        private readonly ISqlSuggarDAL _ISqlSuggarDAL;
        private readonly ITitleDAL _ITitleDAL;
        private readonly IFilesBLL _IFilesBLL;

        public TitleBLL(ISqlSugarClient client, ITitleDAL iTitleDAL, IFilesBLL iFilesBLL, ISqlSuggarDAL iSqlSuggarDAL)
        {
            _ISqlSuggarDAL= iSqlSuggarDAL;
            _ITitleDAL = iTitleDAL;
            _IFilesBLL=iFilesBLL;
        }
         
        public ApiResult addTitle(sys_titleParent obj)
        {
            ApiResult  res = new ApiResult();
            sys_title parent = new sys_title()
            {
                createDate = obj.createDate,
                createUserId = obj.createUserId,
                UpdateUserId = obj.createUserId,
                UpdateDate = obj.UpdateDate,
                Description = obj.Description,
                isCryptonym = obj.isCryptonym

            };
            sys_title ResParent = _ISqlSuggarDAL.Insert<sys_title>(parent);

            if (ResParent.id > 0)
            {

                //插入子数据
                sys_titleMainInfo main = new sys_titleMainInfo();
                main = (sys_titleMainInfo)obj.main;
                main.parentId = ResParent.id;

                sys_titleMainInfo ResMain = _ISqlSuggarDAL.Insert<sys_titleMainInfo>(main);

                List<sys_fileLog> listFiles = obj.files;
                batchfilesLog objFilesLog = new batchfilesLog()
                {
                    parentId = ResParent.id,
                    filesLog = listFiles
                };
                 _IFilesBLL.batchAddFileRecordBLL(objFilesLog, obj.createUserId);

                List<sys_titleTopic> lsitTopic = obj.topic;
                sys_titleTopic topic = new sys_titleTopic();
                for (int i = 0; i < lsitTopic.Count; i++)
                {
                   
                        topic.parentId = ResParent.id;
                        topic.topicId = lsitTopic[i].topicId;

                    sys_titleTopic ResTopic = _ISqlSuggarDAL.Insert<sys_titleTopic>(topic);

                    topic=null;
                }

                res.code = 200;
                res.msg = "添加成功";
                
            }
            else
            {

                res.code = 403;
                res.msg = "添加失败";
             
            }


            return res;
        }
         
        public ApiDataResult<PagingData<titleListDto>> getTitleList(int? typeId, string? name,int pageSize, int pageIndex)
        {
            ApiDataResult<PagingData<titleListDto>> res = new ApiDataResult<PagingData<titleListDto>>();
            List<Condition> funcWhere = null;

            if (name.Length > 0)
            {
                //funcWhere = x => SqlFunc.Like(x.name, $"%{topicName}%");
            }

            Expression<Func<sys_titleParent, object>> funcOrderb = null;
            PagingData<titleListDto> data = _ITitleDAL.getTopicTypeListConditon(funcWhere, pageSize, pageIndex, funcOrderb);
            if ( data.dataList.Count> 0)
            {
                res.code = 200;
                res.msg = "获取成功";
                res.data = data;
            }
            else
            {
                res.code = 200;
                res.msg = "无数据";
                res.data = null;
            }
            return res;
           
        }

        public ApiDataResult<titleDetailDto> getTitleDetailById(int id)
        {
            ApiDataResult<titleDetailDto> res = new ApiDataResult<titleDetailDto>();
           titleDetailDto data = _ITitleDAL.getTitleDetailById(id);
            if (data != null &&data.id ==id)
            {
                res.code = 200;
                res.data = data;
                res.msg = "查询成功";
                 
            }
            else
            {
                res.code = 410;
                res.data = null;
                res.msg = "数据不存在";

            } 
            return res;
        }


    }




}
